<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新品统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-form-label {
            width: 100px;
        }
        .layui-input-inline {
            width: 200px;
        }
        .layui-table-cell {
            text-align: center;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">物料名称</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="materialName" placeholder="请输入物料名称">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始日期</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="startDate" placeholder="请选择开始日期">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束日期</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="endDate" placeholder="请选择结束日期">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"
                                    lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 查询
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="newMaterialSummaryTable" lay-filter="newMaterialSummaryFilter"></table>

        <script type="text/html" id="tableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>
            </div>
        </script>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            util = layui.util;

        // 初始化日期选择器
        laydate.render({
            elem: '#startDate',
            type: 'date'
        });
        laydate.render({
            elem: '#endDate',
            type: 'date'
        });

        // 初始化表格
        table.render({
            elem: '#newMaterialSummaryTable',
            url: '/statistics/newMaterial/summary',
            loading: true,
            toolbar: '#tableToolbar',
            parseData: function(res) {
                return {
                    "code": 0,
                    "msg": "",
                    "count": res.length,
                    "data": res
                };
            },
            cols: [[
                {field: 'materialCode', title: '物料编码', width: 120, align: 'center', sort: true},
                {field: 'materialName', title: '物料名称', width: 150, align: 'center'},
                {field: 'specifications', title: '规格型号', width: 150, align: 'center'}, // 修正字段名
                {field: 'unit', title: '单位', width: 100, align: 'center', templet: function(d) {
                        // 添加单位字典转换
                        var units = {1: '米', 2: '吨', 3: '根', 4: '千克', 5: '桶'};
                        return units[d.unit] || d.unit;
                    }},
                {field: 'currentStock', title: '库存数量', width: 120, align: 'center', sort: true}, // 修正字段名
                {field: 'outQuantity', title: '出库数量', width: 120, align: 'center', sort: true}, // 修正字段名
                {field: 'receiptDate', title: '入库日期', width: 180, align: 'center', // 修正字段名
                    templet: function(d) {
                        return d.receiptDate ? util.toDateString(d.receiptDate, 'yyyy-MM-dd') : '';
                    }
                },
                // 移除不存在的projectName字段
                {field: 'remark', title: '备注', width: 200, align: 'center'}
            ]],
            page: true,
            skin: 'line',
            limit: 10,
            limits: [10, 20, 50]
        });

// 搜索功能
        form.on('submit(data-search-btn)', function (data) {
            var materialName = $('#materialName').val();
            var startDate = $('#startDate').val();
            var endDate = $('#endDate').val();

            // 日期范围验证
            if (startDate && endDate && startDate > endDate) {
                layer.msg('开始日期不能晚于结束日期', {icon: 2});
                return false;
            }

            table.reload('newMaterialSummaryTable', {
                url: '/statistics/newMaterial/summary',
                where: {
                    materialName: materialName,
                    startDate: startDate,
                    endDate: endDate
                },
                page: {
                    curr: 1
                }
            });
            return false;
        });

        // 工具栏事件
        table.on('toolbar(newMaterialSummaryFilter)', function (obj) {
            if (obj.event === 'export') {
                var materialName = $('#materialName').val();
                var startDate = $('#startDate').val();
                var endDate = $('#endDate').val();

                var url = '/statistics/newMaterial/export?' +
                    'materialName=' + encodeURIComponent(materialName || '') +
                    '&startDate=' + encodeURIComponent(startDate || '') +
                    '&endDate=' + encodeURIComponent(endDate || '');

                window.location.href = url;
            }
        });
    });
</script>
</body>
</html>